﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mBlog.Data.Entities;
using System.Data.Objects;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.EntityClient;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;

namespace mBlog.Data
{
    public class Repository<T> : IDisposable, IRepository<T> where T :class
    {
        private mBlogContext objectContext;
        private DbSet<T> dbSet;
        public Repository(mBlogContext objContext)
        {
            objectContext = objContext;
            if (this.objectContext == null)
                throw new InvalidOperationException("DBContext should not be null");

            this.dbSet = objectContext.Set<T>();
        }


        //public void Attach(T t)
        //{
        //    dbSet.Attach(t);
        //}

        public T Add(T t)
        {
            dbSet.Add(t);

            return t;
        }

        //public void MarkAsModified(T t)
        //{
        //    objectContext.MarkAsModified(t);
        //}

        //public IList<object> Read(string storedProcedureName)
        //{
        //    IList<object> results = new List<object>();
        //    using (DbConnection conn = objectContext.GetConnection())
        //    {
        //        using (DbCommand cmd = conn.CreateCommand())
        //        {
        //            cmd.CommandType = System.Data.CommandType.StoredProcedure;
        //            cmd.CommandText = storedProcedureName;

        //            IDataReader reader = cmd.ExecuteReader();
        //            while (reader.Read())
        //            {
        //                results.Add(
        //                    new
        //                    {
        //                        Year = reader["Year"],
        //                        Month = reader["Month"],
        //                        Count = reader["Count"]
        //                    }
        //                    );
        //            }
        //        }
        //    }

        //    return results;
        //}

        public void SaveChanges()
        {
            objectContext.SaveChanges();
        }

        public T Single(Func<T, bool> pred)
        {
            return dbSet.SingleOrDefault(pred);
        }

        public void Delete(T t)
        {
            dbSet.Remove(t);
        }

        public IQueryable<T> Where(Func<T, bool> pred)
        {
            return dbSet.Where(pred).AsQueryable();
        }

        public IQueryable<T> All()
        {
            return dbSet.AsQueryable();
        }

        public void Dispose()
        {
            if (this.objectContext != null)
            {
                this.objectContext.Dispose();
            }
        }


        public T Find(object key)
        {
            return dbSet.Find(key);
        }
    }
}
